﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.Forms;

namespace MobileSMSContact
{
    public partial class frmMain : Form
    {
        OutlookSession outlookSession = new OutlookSession();
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            List<string> categories = GetContactCategories();

            categories.ForEach(delegate(String name)
                {
                    if(!comboBoxCategories.Items.Contains(name.Trim()))
                        comboBoxCategories.Items.Add(name.Trim());
                });   
        }

        private List<string> GetContactCategories()
        {
            List<string> categories = new List<string>();

            ContactCollection contactsCollection = outlookSession.Contacts.Items;

            foreach (Contact contact in contactsCollection)
            {
                categories.AddRange(contact.Categories.Split(','));
            }

            // categories.Sort();

            return categories;
        }

        private void comboBoxCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBoxContacts.Items.Clear();

            List<Contact> contacts = GetContacts(comboBoxCategories.SelectedItem.ToString());

            foreach (Contact contact in contacts)
                listBoxContacts.Items.Add(string.Format("{0} {1}", contact.FirstName, contact.LastName));
        }

        private List<Contact> GetContacts(string category)
        {
            List<Contact> contacts = new List<Contact>();

            ContactCollection contactsCollection = outlookSession.Contacts.Items;

            foreach (Contact contact in contactsCollection)
            {
                if (contact.Categories.Contains(category))
                {
                    contacts.Add(contact);
                }
            }

            // contacts.Sort(delegate(Contact c1, Contact c2) { return c1.LastName.CompareTo(c2.LastName); });

            return contacts;
        }

        private void textBoxSMSMessage_TextChanged(object sender, EventArgs e)
        {
            labelSMSMessage.Text = string.Format("SMS Message ({0} characters left)", 160 - textBoxSMSMessage.Text.Length);
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemSend_Click(object sender, EventArgs e)
        {
            SendSMS();
        }

        private void SendSMS()
        {
            List<Contact> contacts = GetContacts(comboBoxCategories.SelectedItem.ToString());

            SmsMessage smsMessage = new SmsMessage();
            //Set the message body.
            smsMessage.Body = textBoxSMSMessage.Text;

            foreach (Contact contact in contacts)
            {
                string name = string.Format("{0} {1}", contact.FirstName, contact.LastName).Trim();
                string address = contact.MobileTelephoneNumber;

                if (address.Length == 0) break;

                //Add recipients.
                smsMessage.Body = textBoxSMSMessage.Text;
                if (name.Length == 0)
                    smsMessage.To.Add(new Recipient(address));
                else
                    smsMessage.To.Add(new Recipient(name, address));
                smsMessage.RequestDeliveryReport = checkBoxRequestDeliveryReport.Checked;
            }

            if(smsMessage.To.Count == 0) return;

            //Send the SMS message.
            smsMessage.Send();
        }
    }
}